﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;

namespace LabMS.Web.LabManage
{
    public partial class PreViewSubLab : Base.BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            strPagePrivilege.Add("SYSXX");
            if (UserType == "student" || UserType == "teacher")
            {
                modifybtn.Visible = false;
                DeletBtn.Visible = false;
            }
            if (Request.QueryString["SubID"] != null)
            {
                if (!IsPostBack)
                {
                    int SubID;
                    if (int.TryParse(Request.QueryString["SubID"].ToString(), out SubID))
                    {
                        IDHF.Value = SubID.ToString();
                        LabMS.BLL.Lab lab = new LabMS.BLL.Lab();
                        List<LabMS.Model.Dictionary> list = new List<LabMS.Model.Dictionary>();
                        LabMS.Model.Lab labModel = new LabMS.Model.Lab();
                        LabMS.BLL.Location loc = new LabMS.BLL.Location();
                        List<LabMS.Model.Location> locModel = new List<LabMS.Model.Location>();
                        try
                        {
                            labModel = lab.GetModel(SubID);
                        }
                        catch (Exception ex)
                        {
                            throw ex;
                        }

                        LabNumLbl.Text = labModel.Lab_Code;//编号
                        LabNameLbl.Text = labModel.Lab_Name;//名称
                        SetUpDateLbl.Text = string.Format("{0:d}", labModel.Lab_SetupTime);//日期
                        ManagerLbl.Text = labModel.Lab_InCharge;//负责人
                        TelLbl.Text = labModel.Lab_Phone;//电话
                        CurriculumLbl.Text = labModel.Lab_Curriculum;//课程
                        NumLbl.Text = labModel.Lab_ApprovalCode;//批准文号
                        ProfessionLbl.Text = labModel.Lab_Professional;//面向专业
                        InvestmentLbl.Text = labModel.Lab_Investment.ToString();//投资额
                        StatementLbl.Text = labModel.Lab_Statement;//备注

                        locModel = loc.GetModelList(" LabID=" + SubID);
                        float f = 0;
                        for (int i = 0; i < locModel.Count; i++)
                        {
                            if (locModel[i].Location_Area.ToString().Trim() != "")
                            {
                                f += float.Parse(locModel[i].Location_Area);
                            }
                        }
                        if (f == 0)
                        {
                            UsingAreaLbl.Text = "";//使用面积
                        }
                        else
                        {
                            UsingAreaLbl.Text = f.ToString();//使用面积
                        }

                        //绑定实验类别
                        list = DictionaryDropdownlist("111");
                        LabKindDDL.DataSource = list;
                        LabKindDDL.DataBind();
                        LabKindDDL.SelectedValue = labModel.Lab_Type;

                        //绑定实验类型
                        list = DictionaryDropdownlist("112");
                        LabTypeDDL.DataSource = list;
                        LabTypeDDL.DataBind();
                        LabTypeDDL.SelectedValue = labModel.Lab_Type;

                        //绑定实验级别
                        list = DictionaryDropdownlist("113");
                        LabLevelDDL.DataSource = list;
                        LabLevelDDL.DataBind();
                        LabLevelDDL.SelectedValue = labModel.Lab_Level;

                        //绑定奖励级别
                        list = DictionaryDropdownlist("114");
                        AwardLevelDDL.DataSource = list;
                        AwardLevelDDL.DataBind();
                        AwardLevelDDL.SelectedValue = labModel.Lab_AwardLevel;

                        //绑定实验室状态
                        list = DictionaryDropdownlist("115");
                        StatusDDL.DataSource = list;
                        StatusDDL.DataBind();
                        StatusDDL.SelectedValue = labModel.Lab_State;

                        //绑定所属实验室
                        LabDropdownlist();
                        LabDDL.SelectedValue = labModel.Lab_ParentID.ToString();
                    }
                    else
                    {
                        ERRLbl.Text = "系统异常，URL参数不合法！";
                        ERRLbl.Visible = true;
                        modifybtn.Visible = false;
                        DeletBtn.Visible = false;
                    }
                }
            }
            else
            {
                ERRLbl.Text = "系统异常，URL参数不合法！";
                ERRLbl.Visible = true;
                modifybtn.Visible = false;
                DeletBtn.Visible = false;
            }
        }

        //绑定所属实验室 下拉列表框
        protected void LabDropdownlist()
        {
            LabMS.BLL.Lab lab = new LabMS.BLL.Lab();
            DataSet ds = new DataSet();

            ds = lab.GetList("Lab_ParentID is null");
            LabDDL.DataSource = ds;
            LabDDL.DataBind();
        }

        //绑定数据字典 返回List
        protected List<LabMS.Model.Dictionary> DictionaryDropdownlist(string code)
        {
            string strwhere = "DClass='" + code + "'";
            LabMS.BLL.Dictionary dic = new LabMS.BLL.Dictionary();

            List<LabMS.Model.Dictionary> list = new List<LabMS.Model.Dictionary>();
            list = dic.GetModelList(strwhere);
            return list;
        }

        protected void DeletBtn_Click(object sender, EventArgs e)
        {
            LabMS.BLL.Lab isLab = new LabMS.BLL.Lab();
            List<LabMS.Model.Lab> isLabModel = new List<LabMS.Model.Lab>();

            try
            {
                isLabModel = isLab.GetModelList("Lab_ParentID=" + int.Parse(IDHF.Value));
                if (isLabModel.Count > 0)
                {
                    Page.ClientScript.RegisterClientScriptBlock(GetType(), "", "<script type='text/javascript'>alert('系统异常错误！');</script>");
                    return;
                }
                List<string> strID = new List<string>();
                List<string> strTable = new List<string>();
                List<string> strColName = new List<string>();
                #region 实验室
                strID.Add(IDHF.Value);
                strTable.Add("Lab");
                strColName.Add("ID");
                #endregion
                #region 获奖信息
                strID.Add(IDHF.Value);
                strTable.Add("AwardInfo");
                strColName.Add("Award_Lab");
                #endregion
                #region 实验用房
                strID.Add(IDHF.Value);
                strTable.Add("Location");
                strColName.Add("LabID");
                #endregion
                #region 人员信息
                strID.Add(IDHF.Value);
                strTable.Add("Staff");
                strColName.Add("Staff_Lab");
                #endregion
                #region 项目申请书
                strID.Add(IDHF.Value);
                strTable.Add("ProjectApp");
                strColName.Add("LabID");
                #endregion
                #region 岗位职责
                strID.Add(IDHF.Value);
                strTable.Add("Responsibility");
                strColName.Add("Resp_Lab");
                #endregion
                #region 实验项目
                strID.Add(IDHF.Value);
                strTable.Add("Project");
                strColName.Add("Project_Lab");
                #endregion
                #region 课程安排
                strID.Add(IDHF.Value);
                strTable.Add("Arrangement");
                strColName.Add("LabID");
                #endregion
                #region 教学任务书
                strID.Add(IDHF.Value);
                strTable.Add("TeachMission");
                strColName.Add("LabID");
                #endregion
                #region 课程
                strID.Add(IDHF.Value);
                strTable.Add("Course");
                strColName.Add("LabID");
                #endregion
                #region 课程
                strID.Add(IDHF.Value);
                strTable.Add("Evaluation");
                strColName.Add("LabID");
                #endregion
                ExtendBLL.LabInfo labInfo = new ExtendBLL.LabInfo();
                labInfo.DeleteLab(strID, strTable, strColName);
                Page.ClientScript.RegisterClientScriptBlock(GetType(), "", "<script type='text/javascript'>alert('删除成功!');refreshParent();window.close();</script>");
            }
            catch
            {
                Page.ClientScript.RegisterClientScriptBlock(GetType(), "", "<script type='text/javascript'>alert('删除失败!');refreshParent();window.close();</script>");
            }
        }
    }
}
